package HuaWei;

import java.util.*;

/**
 * @author zhang kun
 * @Classname Three
 * @Description TODO
 * @Date 2021/8/29 21:42
 */
public class Three {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()){
            List<ArrayList<Integer>> list = new ArrayList<>();
            int[] time = new int[n];
            int n = scanner.nextInt();
            for(int i = 0;i<n;i++){
                list.add(new ArrayList<Integer>());
            }
            for(int i = 0;i<n;i++){
                String line = scanner.nextLine();
                String[] lines = line.split(",");
                for(int j = 0;j<lines.length-1;j++){
                    int a = Integer.parseInt(lines[j]);
                    list.get(a).add(i);
                }
                String[] splits = lines[lines.length-1].split(" ");
                if(splits[0] != "-1"){
                    list.get(Integer.parseInt(splits[0]));
                }
                int t = Integer.parseInt(splits[1]);
                time[i] = t;
            }
            System.out.println(getMaxs(list,time));
        }
    }

    public static int getMaxs(List<ArrayList<Integer>> list,int[] time){
        int n = time.length;
        int[] ins = new int[n];
        for(int i = 0;i<list.size();i++){
            for(int t : list.get(i)){
                ins[t]++;
            }
        }
        Queue<Integer> queue = new LinkedList<>();
        int[] dp = new int[n];
        for(int i = 0;i<n;i++){
            if(ins[i] == 0){
                queue.add(i);
                dp[0] = time[i];
            }
        }
        while(!queue.isEmpty()){
            Integer poll = queue.poll();
            for(int p : list.get(poll)){

            }
        }
    }
}
